package com.wk.wechattool.tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import java.util.List;

public class BaseTool {
    static  boolean isListEmpyty(List list){
        return list==null || list.size()==0;
    }

    private static String getPackageName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(  context.getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    public static  boolean checkAccessibilityEnabled_2(Context context , Class clazz) throws  Exception {
        String packageName =getPackageName(context) ;
        String serviceName =packageName+"/"+clazz.getName() ;

        int accessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        if( accessibilityEnabled!=1){
            return false ;
        }

        String settingValue = Settings.Secure.getString( context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        if(settingValue ==null){
            return false ;
        }

        String [] args = settingValue.split(":");
        for(String name: args){
            if(serviceName.equalsIgnoreCase(name)){
                return true ;
            }
        }
        return false;
    }


    /**
     *  开启悬浮窗权限
     */
    public static void requestSettingCanDrawOverlays(Activity activity) {
        int REQUEST_DIALOG_PERMISSION =10001 ;
        int sdkInt = Build.VERSION.SDK_INT;
        if (sdkInt >= Build.VERSION_CODES.O) {//8.0以上
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            activity.startActivityForResult(intent, REQUEST_DIALOG_PERMISSION);
        } else if (sdkInt >= Build.VERSION_CODES.M) {//6.0-8.0
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + activity.getPackageName()));
            activity.startActivityForResult(intent, REQUEST_DIALOG_PERMISSION);
        }
        // 6.0 以下，无需处理
    }
}
